package org.apache.dolphinscheduler.api.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
import org.apache.dolphinscheduler.api.aspect.PermissionCheck;
import org.apache.dolphinscheduler.api.dto.coupon.CouponCreateRequest;
import org.apache.dolphinscheduler.api.exceptions.ApiException;
import org.apache.dolphinscheduler.api.service.CouponService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.dao.entity.ChargingRule;
import org.apache.dolphinscheduler.dao.entity.Couponbill;
import org.apache.dolphinscheduler.dao.entity.Coupondetail;
import org.apache.dolphinscheduler.dao.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

import static org.apache.dolphinscheduler.api.enums.Status.*;

/**
 * <p>
 * 收支明细表 前端控制器
 * </p>
 *
 * @author denggh
 * @since 2023-08-14
 */
@Api(tags = "创建算力券 API")
@RestController
@RequestMapping("/operation/coupon")
public class CouponController extends BaseController {

    @Autowired
    private CouponService couponService;


    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @PermissionCheck(permission = "operation:coupon:create")
    public Result createCouponDetail(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                    @RequestBody Coupondetail coupondetail
                                  ) {

        couponService.createCouponDetail(loginUser,coupondetail);
        return success();
    }

    /**
     * 获取算力券订单明细列表
     */
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(LOGIN_USER_QUERY_BILL_LIST_PAGING_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @ApiOperation("获取算力券订单明细列表")
    @PermissionCheck(permission = "operation:coupon:list")
    public Result list(
            @Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
            @RequestParam(value = "text", required = false) String couponName,
            @RequestParam(value = "type", required = false) Integer Type,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam("pageNo") Integer pageNo) {
        Result result = checkPageParams(pageNo, pageSize);
        if (!result.checkResult()) {
            return result;
        }
        return couponService.selectPageCoupondetailList(loginUser,  pageSize, pageNo,couponName,Type);
    }

    /**
     * 删除收费规则
     *
     * @param id 收费规则ID
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除收费规则")
    @PermissionCheck(permission = "operation:coupon:delete")
    public Result remove(@PathVariable int id) {
        Map<String, Object> result = couponService.deleteCouponDetailById(id);
        return returnDataList(result);
    }

    /**
     * 更新算力券发布状态
     */
    @PutMapping("/type")
    @ApiOperation("更新算力券发布状态")
    @PermissionCheck(permission = "operation:coupon:update")
    public Result edit(@Validated @RequestBody Coupondetail coupondetail) {
        Map<String, Object> result = couponService.updateCouponDetail(coupondetail);
        return returnDataList(result);
    }

    /**
     * 获取不同算力券类型数量
     */
    @GetMapping("/ratio/type")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(LOGIN_USER_QUERY_BILL_TOTAL_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @ApiOperation("获取不同算力券类型数量")
    @PermissionCheck(permission = "operation:coupon:detail-graph")
    public Result total(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                        @RequestParam(value = "month", required = true) String month) {
        return couponService.selectCouponTotal(loginUser, month);
    }

    /**
     * 获取算力券金额占比统计
     */
    @GetMapping("/getCouponAmountTotal")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(LOGIN_USER_QUERY_BILL_TOTAL_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @ApiOperation("获取算力券金额统计")
    @PermissionCheck(permission = "operation:coupon:detail-graph")
    public Result getCouponUsedTotal(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                     @RequestParam(value = "month", required = true) String month) {
        return couponService.selectCouponAmountTotal(loginUser, month);
    }
}
